/*
 * Google Map API map object
 */
var map = null;
/*
 * Landmarks container
 */
var marks = new Object();

addEvent("onload", init);
addEvent("onresize", checkOrientation);

var screenmode = 0;

function init(){
    try {
        var myStyleTweaks = new StyleTweaker();
        myStyleTweaks.add("Series60/5.0", "styles/tweaks/S605th.css");
        myStyleTweaks.add("Series60/3.2", "styles/tweaks/S603rdFP2.css");
        myStyleTweaks.tweak();
        
        initService();
        initMap();
        populateCategories();
        listLandmarks();
    } 
    catch (e) {
        alert(e.toString());
    }
}

function checkOrientation(){
    try {
        var width = parseInt(screen.width);
        var height = parseInt(screen.height);
        
        //var landscapeTweaker = new StyleTweaker();
        // Landscape tweaks for template styles not available in templates?
        //landscapeTweaker.add("Series60/5.0", "../resources/styles/tweaks/S605thLandscape.css");
        
        if (width > height) {
        
            // if already in landscape mode, return
            if (screenmode == 1) 
                return;
            screenmode = 1;
            // landscapeTweaker.tweak();
        }
        else {
            // if already in portrait mode, return
            if (screenmode == 0) 
                return;
            screenmode = 0;
            //        landscapeTweaker.untweak();
        }
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Initializes Google Map API map object
 */
function initMap(){
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl3D());
    map.setCenter(new GLatLng(60.164743, 24.945));
	map.setZoom(13);
	
}

/*
 * Shows or hides map container
 * f - flag: if true - map is shown, otherwise landmarksTab is shown
 */
function showMap(f){
    if (f) {
		document.getElementById("a1").className="selectedTab";
		document.getElementById("a2").className="";
        document.getElementById("map").style.display = "";
        document.getElementById("landmarksTab").style.display = "none";
    }
    else {
		document.getElementById("a1").className="";
		document.getElementById("a2").className="selectedTab";
        document.getElementById("map").style.display = "none";
        document.getElementById("landmarksTab").style.display = "block";
    }
}

/*
 * Displays selected landmarks on map
 */
function showSelected(){
    map.clearOverlays();
    var chs = document.f1.ch;
    if (chs == null) return;
    if (chs.length) {
		for (var i = 0; i < chs.length; i++) {
			if (chs[i].checked) {
				showLandmark(chs[i].value);
			}
		}
	}else
		showLandmark(chs.value);
	
    showMap(true);
}

/*
 * Displays single landmark on map
 * landmarkID - unique id of landmark to be shown
 * clear - true/false: specifies if previous landmarks should be cleared from map
 */
function showLandmark(landmarkID, clear){
    if (clear) {
        map.clearOverlays();
        showMap(true);
    }
    var latlng = new GLatLng(marks[landmarkID].position.latitude, marks[landmarkID].position.longitude);
    var marker = new GMarker(latlng);
    GEvent.addListener(marker, "click", function(){
        var popup = "<div align='center'>" + marks[landmarkID].name + "<br />Lat: " + round(marks[landmarkID].position.latitude) + " \u00B0" + "<br />Lon: " + round(marks[landmarkID].position.longitude) + " \u00B0" + "</div>";
        marker.openInfoWindowHtml(popup);
    });
    map.addOverlay(marker);
    map.setCenter(latlng);
}

/*
 * Sets all landmark checkboxes to specified status
 * checked - status to be set: true or false
 */
function chALL(checked){
    var items = document.f1.ch;
    if (items == null) 
        return;
    if (items.length) {
        for (var i = 0; i < items.length; i++) {
            items[i].checked = checked;
        }
    }
    else 
        items.checked = checked;
}

/*
 * Sets 'all' checkbox to 'checked' status if all landmark checkboxes are checked
 */
function checkMain(){
    var items = document.f1.ch;
    if (items == null) 
        return;
    var checked = true;
    if (items.length) {
        for (var i = 0; i < items.length; i++) {
            checked = checked && items[i].checked;
        }
    }
    else 
        checked = items.checked;
    document.f1.chAll.checked = checked;
}

/*
 * Rounds number
 */
function round(v){
    var a = 10000;
    return Math.round(v * a) / a;
}
